const app = getApp()
const Api = require('../../utils/api.js')

Page({

	data: {
		mobile: '',
		forms: [
			{ type: 'number', value: '', name: 'student_id', label: '学号', placeholder: '你的学号' },
			{ type: 'text', value: '', name: 'college_id', label: '学院', placeholder: '你的学院' },
			{ type: 'text', value: '', name: 'major_id', label: '专业', placeholder: '你的专业' },
			{ type: 'text', value: '', name: 'classname', label: '班级', placeholder: '你的班级' },
		]
	},

	onLoad() {
		this.getUserInfo()
	},

	getUserInfo() {
		app.get(Api.user.info).then(res => {
			var forms = []
			forms = this.data.forms.map(item => {
				item.value = res.data[item.name]
				return item
			})
			console.log(forms)
			this.setData({ forms })
		})
	},

	formSubmit(e) {
		console.log(e.detail.value)
		var isUpdate = new Array(this.data.forms.length).fill(false)
		this.data.forms.forEach((item, i) => {
			if (item.value !== e.detail.value[item.name]) {
				isUpdate[i] = true
			}
		})

		if (isUpdate.indexOf(true) >= 0) {
			app.post(Api.user.update, e.detail.value).then(res => {
				if (res.code === 1) {
					app.$msg.success('更新成功')
				} else {
					app.$msg.toast('更新失败: ' + res.msg)
				}
			}).catch(err => {
				app.$msg.toast('更新失败: ' + err)
			})
		} else {
			app.$msg.toast('什么都没有更新')
		}
	}
})